package com.lyl.common.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * @author Mr.Luo
 * @desc 通用返回包装类
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public static <T> Result<T> build(AbstractCode abstractCode) {
        return build(abstractCode.getCode(), abstractCode.getMsg(), null);
    }

    public static <T> Result<T> build(AbstractCode abstractCode, T data) {
        return build(abstractCode.getCode(), abstractCode.getMsg(), data);
    }

    public static <T> Result<T> build(int code, String msg, T data) {
        return new Result<T>()
                .setCode(code)
                .setMsg(msg)
                .setData(data);
    }

    public static <T> Result<T> ok() {
        return build(BaseCode.SUCCESS, null);
    }

    public static <T> Result<T> ok(T data) {
        return build(BaseCode.SUCCESS, data);
    }

    public static <T> Result<T> fail() {
        return build(BaseCode.FAILURE, null);
    }

    public static <T> Result<T> fail(T data) {
        return build(BaseCode.FAILURE, data);
    }
}
